****************
****************
* Replication file for Rival Claims by Bethany Lacina
* Univ of Michigan Press, 2017
* Chapter 4 & Appendix Table A2
****************
****************

use "chp4replication.dta", clear

global controls1 elpoler ehindupc distmsh s1distmsh ntnllitratio lnelpop eupc ldist 

****************
****************
* VIOLENT STATEHOOD MOVEMENTS
****************
****************

****************
*Figure 4.1
****************

preserve 
collapse (count) seatdiff, by(mybin ips)
twoway (bar seatdiff mybin if ips==0, barw(0.8)) ///
(bar seatdiff mybin if ips==1, barw(0.8)) ///
, name(Fig4_1, replace) ///
ytitle("Number of group-year observations") ///
xtitle("Seat difference as % of PM's coalition") ///
legend(lab(1 "No violent" "statehood movement") lab(2 "Violent statehood" "movement")) ///
yscale(log) ylabel(1 10 100 1000 5000)
restore

****************
*Table 4.1, Model 1
****************

logit ips seatadv seatdisadv  $controls1 ipspyrspline* if (casetype==2|casetype==3), vce(robust)

****************
*Figure 4.2
****************

generate m1sample=e(sample)

foreach var in $controls1 {
qui summ `var' if m1sample==1
scalar define cntrl`var'=r(mean)
}

*Preparation for rug plot
gen obs=m1sample*-0.025
keep seatdiff obs
keep if seatdiff>-10 & seatdiff<5
save m1obs.dta, replace

*Make data for simulation
clear
range seatdiff -10 5 499
gen seatadv=seatdiff
replace seatadv=0 if seatadv<0
gen seatdisadv=seatdiff
replace seatdisadv=0 if seatdisadv>0
replace seatdisadv=abs(seatdisadv)

foreach x in $controls1 {
gen `x'=scalar(cntrl`x')
}

foreach var in  ipspyrspline1 ipspyrspline2 ipspyrspline3 ipspyrspline4 { 
gen `var'=0
}

predict hat, xb

predict hat_se, stdp

generate prips=exp(hat)/(1+exp(hat))

gen lb = hat - invnorm(0.90)*hat_se
gen ub = hat + invnorm(0.90)*hat_se
gen prlb = exp(lb)/(1+exp(lb))
gen prub = exp(ub)/(1+exp(ub))

gen obs=.

append using "m1obs.dta"

twoway (line prips seatdiff, sort) ///
(line prlb seatdiff, sort lp(dash) lc(black)) ///
(line prub seatdiff, sort lp(dash) lc(black)) ///
(spike obs seatdiff), ///
ytitle("Probability of violent mobilization") ///
xtitle("Seat difference as % of PM's coalition")  ///
legend(off) name(Fig4_2, replace)

****************
****************
* ETHNOTERRITORIAL INSURGENCY
****************
****************

****************
*Figure 4.3
****************

use "chp4replication.dta", clear

preserve 
collapse (count) seatdiff, by(mybin ucdpi)
twoway (bar seatdiff mybin if ucdpi==0, barw(0.8)) ///
(bar seatdiff mybin if ucdpi==1, barw(0.8)) ///
, name(Fig4_3, replace) ///
ytitle("Number of group-year observations") ///
xtitle("Seat difference as % of PM's coalition") ///
legend(lab(1 "No insurgency") lab(2 "Insurgency")) ///
yscale(log) ylabel(1 10 100 1000 5000)
restore
****************
*Table 4.1, Model 2
****************

logit ucdpi seatadv seatdisadv $controls1 sthd ucdpipyrspline*, vce(robust)

****************
*Figure 4.4
****************

generate m2sample=e(sample)

foreach var in $controls1 sthd {
qui summ `var' if m2sample==1
scalar define cntrl`var'=r(mean)
}

*Preparation for rug plot
gen obs=m2sample*-0.05
keep seatdiff obs
keep if seatdiff>-10 & seatdiff<5
save m2obs.dta, replace

*Make data for simulation
clear
range seatdiff -10 5 499
gen seatadv=seatdiff
replace seatadv=0 if seatadv<0
gen seatdisadv=seatdiff
replace seatdisadv=0 if seatdisadv>0
replace seatdisadv=abs(seatdisadv)

foreach x in $controls1 sthd {
gen `x'=scalar(cntrl`x')
}

foreach var in  ucdpipyrspline1 ucdpipyrspline2 ucdpipyrspline3 ucdpipyrspline4 { 
gen `var'=0
}

predict hat, xb

predict hat_se, stdp

generate prucdpi=exp(hat)/(1+exp(hat))

gen lb = hat - invnorm(0.90)*hat_se
gen ub = hat + invnorm(0.90)*hat_se
gen prlb = exp(lb)/(1+exp(lb))
gen prub = exp(ub)/(1+exp(ub))

gen obs=.

append using "m2obs.dta"

twoway (line prucdpi seatdiff, sort) /// 
(line prlb seatdiff, sort lp(dash) lc(black)) ///
(line prub seatdiff, sort lp(dash) lc(black)) ///
(spike obs seatdiff), ///
ytitle("Probability of insurgency") ///
xtitle("Seat difference as % of PM's coalition")  ///
legend(off) name(Fig4_4, replace)

****************
*Table A.2, Model 8
****************
use "chp4replication.dta", clear
logit ucdpi seatadv seatdisadv $controls1 sthd ucdpipyrspline* if border==1, vce(robust)

****************
*Table A.2, Model 9
****************

logit ucdpi seatadv seatdisadv $controls1 sthd ucdpipyrspline* if border==1 & casetype!=3, vce(robust)

****************
****************
* STATEHOOD
****************
****************

stset year, id(eid) failure(sthd_granted==1) origin(time y0)

stcox ipsma  $controls1

stcox seatdiff $controls1
